Refactor cargo_test into an ops module
authorAlex Crichton <alex@alexcrichton.com>
Fri, 1 Aug 2014 02:33:46 +0000 (19:33 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Tue, 5 Aug 2014 17:32:33 +0000 (10:32 -0700)
The logic for doc tests will get a little complex, so this is moved to aseparate
module instead of inside the executable.

src/bin/cargo-test.rs
src/cargo/ops/cargo_compile.rs
src/cargo/ops/cargo_test.rs [new file with mode: 0644]
src/cargo/ops/mod.rs

index 66c22812b4aaedfb9a618b762c095dcb3ba70cdd..522b77735d248d0d718e6d94be7a035dca853b80 100644 (file)
@@ -8,9 +8,8 @@ extern crate docopt;
 use std::io::process::ExitStatus;
 
 use cargo::ops;
-use cargo::{execute_main_without_stdin};
-use cargo::core::{MultiShell};
-use cargo::util;
+use cargo::execute_main_without_stdin;
+use cargo::core::MultiShell;
 use cargo::util::{CliResult, CliError, CargoError};
 use cargo::util::important_paths::{find_root_manifest_for_cwd};
 
@@ -48,24 +47,18 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
         target: None,
     };
 
-    let test_executables = try!(ops::compile(&root,
-                                             &mut compile_opts).map_err(|err| {
+    let err = try!(ops::run_tests(&root, &mut compile_opts,
+                                  options.arg_args.as_slice()).map_err(|err| {
         CliError::from_boxed(err, 101)
     }));
-
-    let test_dir = root.dir_path().join("target").join("test");
-
-    for file in test_executables.iter() {
-        try!(util::process(test_dir.join(file.as_slice()))
-                  .args(options.arg_args.as_slice())
-                  .exec().map_err(|e| {
-            let exit_status = match e.exit {
+    match err {
+        None => Ok(None),
+        Some(err) => {
+            let status = match err.exit {
                 Some(ExitStatus(i)) => i as uint,
-                _ => 1,
+                _ => 101,
             };
-            CliError::from_boxed(e.mark_human(), exit_status)
-        }));
+            Err(CliError::from_boxed(err.mark_human(), status))
+        }
     }
-
-    Ok(None)
 }
index bdb71707eb5f9764e12bf220c2336781b4f18cdc..dabbd8e969bc0d1e97eaf184ee8950ce55b8200f 100644 (file)
@@ -44,7 +44,7 @@ pub struct CompileOptions<'a> {
 }
 
 pub fn compile(manifest_path: &Path,
-               options: &mut CompileOptions) -> CargoResult<Vec<String>> {
+               options: &mut CompileOptions) -> CargoResult<()> {
     let CompileOptions { update, env, ref mut shell, jobs, target } = *options;
     let target = target.map(|s| s.to_string());
 
@@ -127,18 +127,7 @@ pub fn compile(manifest_path: &Path,
 
     try!(ops::write_resolve(&package, &resolve));
 
-    let test_executables: Vec<String> = targets.iter()
-        .filter_map(|target| {
-            if target.get_profile().is_test() {
-                debug!("Run  Target: {}", target.get_name());
-                Some(target.file_stem())
-            } else {
-                debug!("Skip Target: {}", target.get_name());
-                None
-            }
-    }).collect();
-
-    Ok(test_executables)
+    Ok(())
 }
 
 fn source_ids_from_config(configs: &HashMap<String, config::ConfigValue>,
diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs
new file mode 100644 (file)
index 0000000..6828235
--- /dev/null
@@ -0,0 +1,33 @@
+use core::Source;
+use sources::PathSource;
+use ops;
+use util::{process, CargoResult, ProcessError};
+
+pub fn run_tests(manifest_path: &Path,
+                 options: &mut ops::CompileOptions,
+                 args: &[String]) -> CargoResult<Option<ProcessError>> {
+    let mut source = PathSource::for_path(&manifest_path.dir_path());
+    try!(source.update());
+    let package = try!(source.get_root_package());
+
+    try!(ops::compile(manifest_path, options));
+
+    let mut exes = package.get_targets().iter().filter_map(|target| {
+        if !target.get_profile().is_test() { return None }
+        let root = package.get_root().join("target");
+        let root = match target.get_profile().get_dest() {
+            Some(dest) => root.join(dest),
+            None => root,
+        };
+        Some(root.join(target.file_stem()))
+    });
+
+    for exe in exes {
+        match process(exe).args(args).exec() {
+            Ok(()) => {}
+            Err(e) => return Ok(Some(e))
+        }
+    }
+
+    Ok(None)
+}
index ba69961b9bba325086775cc3b643061e441198c2..975ebcf2e289b84ada2bd9923088244966a0cd67 100644 (file)
@@ -7,6 +7,7 @@ pub use self::cargo_new::{new, NewOptions};
 pub use self::cargo_doc::{doc, DocOptions};
 pub use self::cargo_generate_lockfile::{generate_lockfile, write_resolve};
 pub use self::cargo_generate_lockfile::{update_lockfile, load_lockfile};
+pub use self::cargo_test::run_tests;
 
 mod cargo_clean;
 mod cargo_compile;
@@ -16,3 +17,4 @@ mod cargo_run;
 mod cargo_new;
 mod cargo_doc;
 mod cargo_generate_lockfile;
+mod cargo_test;